//
//  DViewController.m
//  ZhiBoYuan
//
//  Created by mac on 2018/4/12.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "DViewController.h"
#import "AAcell.h"
#import "DCell.h"
#import "GeRenSheZhiViewController.h"
#import "MyKeChengViewController.h"
#import "BanJiTableViewController.h"
#import "GuanZhuViewController.h"
#import "ShouCangViewViewController.h"
#import "MyQianBaoViewController.h"
//#import "MyMessageViewController.h"
#import "XiaoXIViewController.h"

#import "SheZhiViewController.h"
#import "ChongZhiViewController.h"

//static NSString * ident = @"DCell";

#import "WoTableViewCell.h"

static NSString *identfire = @"WoTableViewCell";

#import "WoHeaderCell.h"
static NSString *identHead = @"WoHeaderCell";


@interface DViewController ()
{
    UIButton *button;
}

@property (nonatomic,strong)NSMutableArray *selectorPatnArray;//存放选中数据

@end

@implementation DViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    [Helper setMoRenNav:self.navigationController];
    [Helper setNavColorFrom:self.navigationController color:ZhuTiColor];
    
//    self.dataArray = @[
//                       @[],
//                       @[@"我的课程",@"我的钱包",@"我的班级",@"我的关注",@"我的收藏",@"我的消息",@"我的优惠券"],
//                        @[@"设置",@"分享App"]
//                       ].mutableCopy;
    
    
    self.dataArray = @[
                       @[],
                       @[@"我的课程",@"我的钱包",@"我的班级",@"我的关注",@"我的收藏",@"我的消息"],
                       @[@"设置"]
                       ].mutableCopy;
    
    
    [self.view addSubview:self.tableView];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [self loadUnReadMessage];
    
    [self loadUserData];
}


-(void)loadUnReadMessage{
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{@"max_news_id":[NSString stringWithFormat:@"%@",[ZYUserManager userInfo].unreadMessageId]
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Ucenter&a=newsCount",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"status"] intValue] == 200) {
            UserinfoModel *model = [ZYUserManager userInfo];
            model.news_count = [responseObject[@"data"] valueForKey:@"news_count"];
            [ZYUserManager saveUserInfo:model];
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showTextOnly:self.view message:responseObject[@"msg"] delay:2.0f];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

-(void)loadUserData{
    SGParameter *parameter  = [[SGParameter alloc] init];
    
    parameter.body = @{
                       };
    
    NSString *lastUrl  = @"g=Client&m=Ucenter&a=getUserInfo";
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@%@",HTTPP,lastUrl] parameter:parameter sucess:^(id responseObject) {
        
        NSDictionary *dic = responseObject;
        NSLog(@"%@",dic);
        if ([dic[@"status"] intValue] == 200) {
            
            UserinfoModel *model = [ZYUserManager userInfo];
            model.left_xuedou = [NSString stringWithFormat:@"%@",[dic[@"data"][@"user_info"] valueForKey:@"left_xuedou"]];
            
            [ZYUserManager saveUserInfo:model];
            
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:2.0];
            
        }
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,15)];
    
    view.backgroundColor = hui1Color;
    
    return view;
    
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0;
    }
    return 15;
}

#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else if (section == 1){
        
        NSArray *arr =self.dataArray[section];
        return arr.count;
    }else{
        return 1;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        WoHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:identHead forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        NSString *headUrl = [ZYUserManager userInfo].small_img;
                
         [cell.imageHead sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",headUrl]] placeholderImage:[UIImage imageNamed:@"图层18"]];
        cell.nmaLlabel.text = [ZYUserManager userInfo].nick_name;
        
        cell.labelB.text = [NSString stringWithFormat:@"%@",[ZYUserManager userInfo].left_xuedou];
        
//充值
        [cell.chonZhiButton addTarget:self action:@selector(chongzhiAction) forControlEvents:(UIControlEventTouchUpInside)];
        return cell;
    }else  if (indexPath.section == 1){
        WoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfire forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        if (indexPath.row ==5 ) {
            
            if ([[ZYUserManager  userInfo].news_count isEqualToString:@"0"]) {
                cell.view.backgroundColor = [UIColor clearColor];
               
            }else{
                cell.view.backgroundColor = [UIColor redColor];
                 cell.view.text = [ZYUserManager  userInfo].news_count;
            }

        }else{
            cell.view.hidden = YES;
            
        }
        
        
        
        cell.label.text = self.dataArray[indexPath.section][indexPath.row];
        
        return cell;
    }else{
        WoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfire forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        cell.view.hidden = YES;
  
        
         cell.label.text = self.dataArray[indexPath.section][indexPath.row];
        
        
        return cell;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
           return 100;
    }else{
            return 60;
    }
    

    

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        GeRenSheZhiViewController *gerenVC = [[GeRenSheZhiViewController alloc]init];
        gerenVC.hidesBottomBarWhenPushed = YES;
        
        [self.navigationController pushViewController:gerenVC animated:YES];
        
    }else if (indexPath.section == 1) {
        
        if (indexPath.row == 0) {
            MyKeChengViewController *keCHengVC = [[MyKeChengViewController alloc]init];
            keCHengVC.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:keCHengVC animated:YES];
        }else if (indexPath.row == 1){
            MyQianBaoViewController *QianBaoVC = [[MyQianBaoViewController alloc]init];
            QianBaoVC.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:QianBaoVC animated:YES];
            
        }else if (indexPath.row == 2){
            BanJiTableViewController *banJiVC = [[BanJiTableViewController alloc]init];
            banJiVC.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:banJiVC animated:YES];
            
        }else if (indexPath.row == 3){
            GuanZhuViewController *guanzhuVC = [[GuanZhuViewController alloc]init];
            guanzhuVC.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:guanzhuVC animated:YES];
            
        }else if (indexPath.row == 4){
            ShouCangViewViewController *shoucangVC = [[ShouCangViewViewController alloc]init];
            shoucangVC.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:shoucangVC animated:YES];
            
        }else if (indexPath.row == 5){
//            MyMessageViewController *myMessageVC = [[MyMessageViewController alloc]init];
//            myMessageVC.hidesBottomBarWhenPushed = YES;
//
//            [self.navigationController pushViewController:myMessageVC animated:YES];
            
            XiaoXIViewController *messageVC = [[XiaoXIViewController alloc]init];
            messageVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:messageVC animated:YES];
            
        }
        
    }else if (indexPath.section == 2 ){
        if (indexPath.row == 0) {
            //设置
            SheZhiViewController *shezhiVC = [[SheZhiViewController alloc]init];
            shezhiVC.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:shezhiVC animated:YES];
        }
        
    }

    
    NSLog(@"%zd",indexPath.row);

}



#pragma makr -- property
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *  tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, Screenheight- NAVIGATION_BAR_HEIGHT -TAB_BAR_HEIGHT) style:UITableViewStylePlain];
        tableView.backgroundColor = [UIColor clearColor];
        tableView.delegate        = self;
        tableView.dataSource      = self;
        tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        tableView.estimatedRowHeight = 44.0f;//推测高度，必须有，可以随便写多少
        
        tableView.rowHeight = UITableViewAutomaticDimension;//iOS8之后默认就是这个值，可以省略
        
//        tableView.scrollEnabled = NO;
        
         tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        
        //        self.extendedLayoutIncludesOpaqueBars = YES;
        
        if (@available(iOS 11.0, *)) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
        tableView.scrollIndicatorInsets = _tableView.contentInset;
        
        [tableView registerClass:[WoTableViewCell class] forCellReuseIdentifier:identfire];
         [tableView registerClass:[WoHeaderCell class] forCellReuseIdentifier:identHead];
        
        
        [tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];

        _tableView = tableView;
        
    }
    return _tableView;
}

//充值事件
-(void)chongzhiAction{
    ChongZhiViewController *chongHZiVC = [[ChongZhiViewController alloc]init];
    chongHZiVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:chongHZiVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
